/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.example.omsdkdemo;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.device.idea.om.controller.model.StatusBarItem;
import com.example.omsdkdemo.dialog.AppUsageDialogFragment;
import com.example.omsdkdemo.dialog.DeviceDialogFragment;
import com.example.omsdkdemo.manager.OmSdkManager;
import com.example.omsdkdemo.receiver.DeviceReceiver;

/**
 * OmDemoActivity
 *
 * @since 2023-02-02
 */
public class OmDemoActivity extends AppCompatActivity {
    private static final String TAG = "OmSdkDemo";

    private OmSdkManager omSdkManager;

    private DeviceReceiver deviceReceiver;

    private Button btInitSdk;

    private Button autoOpenOn;

    private Button autoOpenOff;

    private Button hideOn;

    private Button hideOff;

    private Button getBrightness;

    private Button getPersistentApp;

    private Button addPersistentApp;

    private Button removePersistentApp;

    private Button getSingleApp;

    private Button addSingleApp;

    private Button removeSingleApp;

    private Button btAppUsage;

    private Button btGetDeviceInfo;

    private Button btClearPackageData;

    private Button btAddDisallowedUninstallPackages;

    private Button btRemoveDisallowedUninstallPackages;

    private Button btGetDisallowedUninstallPackageList;

    private Button installApplication;

    private Button uninstallApplication;

    private Button setDefaultLauncher;

    private Button clearDefaultLauncher;

    private Button getNtpInfo;

    private EditText setNtpInfo;

    private Button setNtp;

    private Button reboot;

    private Button shutdown;

    private Button sleep;

    private Button vol;

    private Button setVol;

    private EditText volValue;

    private Button mic;

    private Button sdkVersion;

    private Button platformVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, this + " onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        omSdkManager = OmSdkManager.getInstance(this);
        initListener();
        registerDeviceReceiver();
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, this + " onDestroy");
        omSdkManager.destroy();
        unregisterDeviceReceiver();
        super.onDestroy();
    }

    private void initListener() {
        btInitSdk.setOnClickListener(v -> initSdk());
        btAppUsage.setOnClickListener(v -> {
            AppUsageDialogFragment appUsageDialogFragment = new AppUsageDialogFragment(omSdkManager);
            appUsageDialogFragment.show(getSupportFragmentManager(), "AppUsageDialogFragment");
        });
        btGetDeviceInfo.setOnClickListener(v -> {
            DeviceDialogFragment deviceDialogFragment = new DeviceDialogFragment(omSdkManager);
            deviceDialogFragment.show(getSupportFragmentManager(), "DeviceDialogFragment");
        });

        initOmServiceListener();
        initDeviceListener();
        initPackageListener();
        initTimeListener();
    }

    private void registerDeviceReceiver() {
        deviceReceiver = new DeviceReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SHUTDOWN);
        intentFilter.addAction(Intent.ACTION_DREAMING_STARTED);
        intentFilter.addAction(Intent.ACTION_DREAMING_STOPPED);
        registerReceiver(deviceReceiver, intentFilter);
    }

    private void unregisterDeviceReceiver() {
        if (deviceReceiver != null) {
            unregisterReceiver(deviceReceiver);
            deviceReceiver = null;
        }
    }

    private void initPackageListener() {
        hideOn.setOnClickListener(v -> omSdkManager
            .showSystemStatusBar(StatusBarItem.STATUS_BAR_ITEM_BACK_BUTTON | StatusBarItem.STATUS_BAR_ITEM_HOME));

        hideOff.setOnClickListener(v -> omSdkManager.showSystemStatusBar(
            StatusBarItem.STATUS_BAR_ITEM_NOTIFY_STATUS_ICON | StatusBarItem.STATUS_BAR_ITEM_SIDEBAR
                | StatusBarItem.STATUS_BAR_ITEM_BACK_BUTTON | StatusBarItem.STATUS_BAR_ITEM_HOME));

        installApplication.setOnClickListener(v -> omSdkManager.installPackage());
        uninstallApplication.setOnClickListener(v -> omSdkManager.uninstallPackage());
        setDefaultLauncher.setOnClickListener(v -> omSdkManager.setDefaultLauncher());
        clearDefaultLauncher.setOnClickListener(v -> omSdkManager.restoreDefaultLauncher());

        getPersistentApp.setOnClickListener(v -> omSdkManager.getPersistentApp());
        addPersistentApp.setOnClickListener(v -> omSdkManager.addPersistentApp());
        removePersistentApp.setOnClickListener(v -> omSdkManager.removePersistentApp());

        getSingleApp.setOnClickListener(v -> omSdkManager.getSingleApp());
        addSingleApp.setOnClickListener(v -> omSdkManager.addSingleApp());
        removeSingleApp.setOnClickListener(v -> omSdkManager.clearSingleApp());

        btAddDisallowedUninstallPackages.setOnClickListener(v -> omSdkManager.addDisallowedUninstallPackages());
        btRemoveDisallowedUninstallPackages.setOnClickListener(v -> omSdkManager.removeDisallowedUninstallPackages());
        btGetDisallowedUninstallPackageList.setOnClickListener(v -> omSdkManager.getDisallowedUninstallPackageList());
        btClearPackageData.setOnClickListener(v -> omSdkManager.clearPackageData());
    }

    private void initDeviceListener() {
        shutdown.setOnClickListener(view -> omSdkManager.shutdown());
        reboot.setOnClickListener(view -> omSdkManager.reboot());
        sleep.setOnClickListener(view -> omSdkManager.sleep());
        vol.setOnClickListener(view -> omSdkManager.getSpeakerVolume());
        setVol.setOnClickListener(view -> setVol());
        mic.setOnClickListener(view -> omSdkManager.setMicSwitch());
    }

    private void initTimeListener() {
        getNtpInfo.setOnClickListener(view -> omSdkManager.getNtpInfo());
        setNtp.setOnClickListener(view -> setNtp());
    }

    private void initOmServiceListener() {
        sdkVersion.setOnClickListener(view -> omSdkManager.getSdkVersion());
        platformVersion.setOnClickListener(view -> omSdkManager.getPlatformVersion());
    }

    private void initSdk() {
        // 通过按钮初始化，用于定位初始化问题
        omSdkManager.initController();
        setViewVisibility(View.VISIBLE);
        btInitSdk.setVisibility(View.GONE);
    }

    private void initView() {
        btInitSdk = findViewById(R.id.bt_init_sdk);
        autoOpenOn = findViewById(R.id.bt_auto_open_on);
        autoOpenOff = findViewById(R.id.bt_auto_open_off);
        hideOn = findViewById(R.id.bt_set_hide_on);
        hideOff = findViewById(R.id.bt_set_hide_off);
        getBrightness = findViewById(R.id.bt_get_brightness);
        getPersistentApp = findViewById(R.id.bt_get_persistent_app);
        addPersistentApp = findViewById(R.id.bt_add_persistent_app);
        removePersistentApp = findViewById(R.id.bt_remove_persistent_app);
        getSingleApp = findViewById(R.id.bt_get_single_app);
        addSingleApp = findViewById(R.id.bt_add_single_app);
        removeSingleApp = findViewById(R.id.bt_remove_single_app);
        btAppUsage = findViewById(R.id.bt_app_usage);
        btGetDeviceInfo = findViewById(R.id.bt_get_device_info);
        btClearPackageData = findViewById(R.id.bt_clear_package_data);
        btAddDisallowedUninstallPackages = findViewById(R.id.bt_add_disallowed_uninstall_packages);
        btRemoveDisallowedUninstallPackages = findViewById(R.id.bt_remove_disallowed_uninstall_packages);
        btGetDisallowedUninstallPackageList = findViewById(R.id.bt_get_disallowed_uninstall_package_list);
        installApplication = findViewById(R.id.bt_install_app);
        uninstallApplication = findViewById(R.id.bt_uninstall_app);
        setDefaultLauncher = findViewById(R.id.bt_set_launcher);
        clearDefaultLauncher = findViewById(R.id.bt_clear_launcher);
        getNtpInfo = findViewById(R.id.conf_create_signed);
        setNtpInfo = findViewById(R.id.user_input_confid);
        setNtp = findViewById(R.id.conf_create_set_vol);
        reboot = findViewById(R.id.conf_create_reboot);
        shutdown = findViewById(R.id.conf_create_shutdown);
        sleep = findViewById(R.id.conf_create_sleep);
        vol = findViewById(R.id.conf_create_vol);
        setVol = findViewById(R.id.conf_create_set_vol1);
        volValue = findViewById(R.id.user_input_confid_value);
        mic = findViewById(R.id.conf_create_mic);
        sdkVersion = findViewById(R.id.sdk_version);
        platformVersion = findViewById(R.id.platform_version);
        setViewVisibility(View.GONE);
    }

    private void setViewVisibility(int visibility) {
        // 未实现的功能不更新
        autoOpenOn.setVisibility(View.GONE);
        autoOpenOff.setVisibility(View.GONE);
        hideOn.setVisibility(visibility);
        hideOff.setVisibility(visibility);
        getBrightness.setVisibility(View.GONE);
        getPersistentApp.setVisibility(View.GONE);
        addPersistentApp.setVisibility(View.GONE);
        removePersistentApp.setVisibility(View.GONE);
        getSingleApp.setVisibility(View.GONE);
        addSingleApp.setVisibility(View.GONE);
        removeSingleApp.setVisibility(View.GONE);
        btAppUsage.setVisibility(visibility);
        btGetDeviceInfo.setVisibility(visibility);
        btClearPackageData.setVisibility(View.GONE);
        btAddDisallowedUninstallPackages.setVisibility(View.GONE);
        btRemoveDisallowedUninstallPackages.setVisibility(View.GONE);
        btGetDisallowedUninstallPackageList.setVisibility(View.GONE);
        installApplication.setVisibility(visibility);
        uninstallApplication.setVisibility(visibility);
        setDefaultLauncher.setVisibility(visibility);
        clearDefaultLauncher.setVisibility(visibility);
        getNtpInfo.setVisibility(visibility);
        setNtp.setVisibility(visibility);
        reboot.setVisibility(visibility);
        shutdown.setVisibility(visibility);
        sleep.setVisibility(visibility);
        vol.setVisibility(visibility);
        setVol.setVisibility(visibility);
        volValue.setVisibility(visibility);
        mic.setVisibility(visibility);
        sdkVersion.setVisibility(visibility);
        platformVersion.setVisibility(visibility);
        setNtpInfo.setVisibility(visibility);
    }

    private void setNtp() {
        String string = String.valueOf(setNtpInfo.getText());
        if (TextUtils.isEmpty(string)) {
            Toast.makeText(this, "请输入NTP地址", Toast.LENGTH_SHORT).show();
            return;
        }
        omSdkManager.setNtpInfo(string);
    }

    private void setVol() {
        String string = String.valueOf(volValue.getText());
        if (TextUtils.isEmpty(string)) {
            Toast.makeText(this, "请输入正确音量值，范围0-15", Toast.LENGTH_SHORT).show();
            return;
        }
        omSdkManager.setSpeakerVolume(string);
    }
}